﻿using CatUtils.Utils.Currency;
using System;
using System.Collections.Generic;
using WordPressSharp;
using WordPressSharp.Models;

namespace CatUtils.Utils.SpecialUtils.Wordpress
{/// <summary>
/// wp发布工具
/// </summary>
    public static class WordpressPublish
    {/// <summary>
     /// 创建发布POST
     /// </summary>
     /// <param name="Title"></param>
     /// <param name="Content"></param>
     /// <returns></returns>
        public static Post CreatePost(string Title, string Content)
        {
            var post = new Post
            {
                PostType = "post",
                Title = Title,
                Content = Content,//在下面获取到img的地方二次赋值了
                PublishDateTime = DateTime.Now.AddHours(-12),
                Status = "publish",
            };

            return post;
        }

        /// <summary>
        ///   发布
        /// </summary>
        /// <param name="post"></param>
        /// <param name="wordPressClient"></param>
        /// <returns></returns>
        public static string PublishConent(Post post, WordPressClient wordPressClient)
        {
            try
            {
                return wordPressClient.NewPost(post);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                return "";
            }
        }

        /// <summary>
        /// 普通发布到默认分类
        /// </summary>
        /// <param name="post"></param>
        /// <param name="localUserInfo"></param>
        /// <returns></returns>
        public static string PublishConent(Post post, WordpressHelper.UserInfo localUserInfo)
        {
            WordPressClient wordPressClient = new WordpressHelper().InitClient(localUserInfo);
            return PublishConent(post, wordPressClient);
        }

        /// <summary>
        /// 发布到指定分类
        /// </summary>
        /// <param name="post"></param>
        /// <param name="category"></param>
        /// <param name="localUserInfo"></param>
        /// <returns></returns>
        public static string PublishConent(Post post, List<int> category, WordpressHelper.UserInfo localUserInfo)
        {
            try
            {
                List<Term> TremsTemp = new List<Term>();
                WordPressClient wordPressClient = new WordpressHelper().InitClient(localUserInfo);
                foreach (int count in category)
                {
                    try
                    {
                        TremsTemp.Add(wordPressClient.GetTerm("category", count));//如果错误出现在这里就代表分类id写错了
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                    }
                }
                post.Terms = TremsTemp.ToArray();
                return PublishConent(post, wordPressClient);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                return "";
            }
        }

        /// <summary>
        /// 发布到指定分类
        /// </summary>
        /// <param name="post"></param>
        /// <param name="CoverImg"></param>
        /// <param name="category"></param>
        /// <param name="localUserInfo"></param>
        /// <param name="CoverImgRelpace"></param>
        /// <returns></returns>
        public static string PublishConent(Post post, string CoverImg, List<int> category, WordpressHelper.UserInfo localUserInfo, bool CoverImgRelpace = true)
        {
            try
            {
                List<Term> TremsTemp = new List<Term>();
                WordPressClient wordPressClient = new WordpressHelper().InitClient(localUserInfo);

                UploadResult TempImg = UpLoadImg(CoverImg, wordPressClient);
                if (TempImg != null)
                {
                    if (CoverImgRelpace)
                    {
                        post.Content = post.Content.Replace(CoverImg, TempImg.Url);
                    }
                    else
                    {
                        post.Content = post.Content + string.Format("<img src='{0}'>", TempImg.Url);
                    }
                }

                foreach (int count in category)
                {
                    try
                    {
                        TremsTemp.Add(wordPressClient.GetTerm("category", count));//如果错误出现在这里就代表分类id写错了
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                    }
                }
                post.Terms = TremsTemp.ToArray();
                return PublishConent(post, wordPressClient);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        private static UploadResult UpLoadImg(string CoverImg, WordPressClient wordPressClient)
        {
            UploadResult img = null;
            try
            {
                img = wordPressClient.UploadFile(Data.CreateFromUrl(CoverImg));
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return img;
        }
    }
}